"use strict";
var _rootJson = null;

async function getDataCount() {
	var rj = await getRootJson();
	return rj.data.length;
}

async function getRootJson() {
	var result = await chrome.storage.local.get("data");	// 88
	_rootJson = result.data;
	if (!_rootJson) {
		_rootJson = {version: 2, data: []};
	}
	return _rootJson;
}

async function setRootJson(data) {
	if (data && data.version == 1) {
		for (var item of data.data) {
			if (item.host != "*") {
		        var pslHost = psl.get(item.host);
				if (pslHost !== null) {
					item.host = pslHost;
				}
			}
		}
		data.version = 2;
	}
	_rootJson = data;
	return await chrome.storage.local.set({data: data});	// 88
}

function newGuid() {
	return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c =>
		(c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
	);
}

function isObjEqual(obj1, obj2) {
	return JSON.stringify(obj1) === JSON.stringify(obj2);
}

function hasOption(data,optname) {
	if (!data) {
		return false;
	}
	if (!("options" in data)) { //!data.hasOwnProperty("options")
		return false;
	}
	if (typeof data["options"] != "string") {
		return false;
	}
	var arrOptions = data["options"].split(" ");
	return (arrOptions.indexOf(optname) > -1);
}

function showError(actionName,error) {
	console.log(actionName + " failed:", error.message);
}
